using System.Windows.Forms;

namespace ctsu.BaseForms
{
	public class Scroll
	{
		public int MaximumVertical;
		public int MaximumHorizontal;
		private Panel _pnl1;
		private Panel _pnl2;
		private VScrollBar _vScroll;
		private HScrollBar _hScroll;

		public Scroll(int maxV, int maxH, Panel pnl1, Panel pnl2,
			VScrollBar vScroll, HScrollBar hScroll)
		{
			MaximumVertical = maxV;
			MaximumHorizontal = maxH;
			this._pnl1 = pnl1;
			this._pnl2 = pnl2;
			this._vScroll = vScroll;
			this._hScroll = hScroll;
		}

		public void CalculateScrolls()
		{
			bool bNeedHoriz = false;

			_hScroll.Visible = false;
			_vScroll.Visible = false;
			DovScrollCalc(out bNeedHoriz);
			DohScrollCalc(bNeedHoriz);
		}

		private void DohScrollCalc(bool bNeedHoriz)
		{
			if (!bNeedHoriz)
			{
				_pnl2.Left = 0;
				_hScroll.Value = 0;
				return;
			}
			_hScroll.Visible = true;

			if (_vScroll.Visible)
			{
				_hScroll.Maximum = MaximumHorizontal + 16;
				_hScroll.Width = _pnl1.Width - 16;
			}
			else
			{
				_hScroll.Maximum = MaximumHorizontal;
				_hScroll.Width = _pnl1.Width;
			}
			_hScroll.LargeChange = _pnl1.Width;
			//Console.WriteLine("hScroll.LargeChange: " + _hScroll.LargeChange);

			if (_pnl1.Width - _pnl2.Left > _hScroll.Maximum)
				_pnl2.Left = _pnl1.Width - _hScroll.Maximum;

			//Console.WriteLine("pnl2.Left: " + _pnl2.Left);
		}

		private void DovScrollCalc(out bool bNeedHoriz)
		{
			bNeedHoriz = false;
			//Console.WriteLine("pnl1.Height: " + _pnl1.Height);
			//Console.WriteLine("pnl1.Width: " + _pnl1.Width);

			//don't need either
			if (_pnl1.Height > MaximumVertical - 1 && _pnl1.Width > MaximumHorizontal - 1)
			{
				_pnl2.Top = 0;
				_vScroll.Visible = false;
				_vScroll.Value = 0;
				bNeedHoriz = false;
				return;
			}

			//don't need vertical
			if (_pnl1.Width < MaximumHorizontal && _pnl1.Height > MaximumVertical + 15)
			{
				_pnl2.Top = 0;
				_vScroll.Visible = false;
				_vScroll.Value = 0;
				bNeedHoriz = true;
				return;
			}

			if (_pnl1.Width < MaximumHorizontal + 16)
				bNeedHoriz = true;

			_vScroll.Visible = true;
			_vScroll.Height = _pnl1.Height;

			if (_pnl1.Width < MaximumHorizontal + 16)
				_vScroll.Maximum = MaximumVertical + 16;
			else
				_vScroll.Maximum = MaximumVertical;

			_vScroll.LargeChange = _pnl1.Height;
			//Console.WriteLine("vScroll.LargeChange: " + _vScroll.LargeChange);

			if (_pnl1.Height - _pnl2.Top > _vScroll.Maximum)
				_pnl2.Top = _pnl1.Height - _vScroll.Maximum;

			//Console.WriteLine("pnl2.top: " + _pnl2.Top);
		}

		public void hScroll_ValueChanged(object sender, System.EventArgs e)
		{
			//Console.WriteLine("hScroll.Value: " + _hScroll.Value);
			_pnl2.Left = -_hScroll.Value;
		}

		public void vScroll_ValueChanged(object sender, System.EventArgs e)
		{
			//Console.WriteLine("vScroll.Value: " + _vScroll.Value);
			_pnl2.Top = -_vScroll.Value;
		}
	}
}
